package cascade.controller.pages.commands
{
	import cascade.App;
	import cascade.model.Page;
	
	import flight.commands.AsyncCommand;
	import flight.net.RemoteService;
	
	import mx.rpc.Fault;

	public class CommandRenamePage extends AsyncCommand
	{
		
		protected var page:Page;
		protected var oldName:String;
		protected var newName:String;
		
		public function CommandRenamePage(page:Page, newName:String)
		{
			this.page = page;
			this.newName = newName;
			this.oldName = page.name;
		}
		
		public override function execute():*
		{
			Cascade.busy.addBusy(this);
			
			Cascade.service.SiteService.renamePage(page.id, newName, result, fault);
		}
		
		public function result(pages:Array):void {
			var index:Number = Cascade.model.pages.getItemIndex(page);
			
			for (var i:Number = 0; i < pages.length; i++) {
				var affectedPage:Page = Cascade.model.pages[index + i] as Page;
				affectedPage.setVO(Page(pages[i]).getVO());
			}
			
			Cascade.busy.removeBusy(this);
			
			dispatchExecutionComplete();
		}
		
		protected function fault(info:Object):void {
			trace(info);
			Cascade.busy.removeBusy(this);
		}
	}
}